package com.yanglanting.disk.role.control;

import com.yanglanting.disk.menu.exception.MenuOccupyException;
import com.yanglanting.disk.mybatis.entity.PtRoleEntity;
import com.yanglanting.disk.mybatis.entity.PtUserEntity;
import com.yanglanting.disk.mybatis.flexigrid.PagingEntity;
import com.yanglanting.disk.mybatis.flexigrid.TblResponseEntity;
import com.yanglanting.disk.mybatis.ztree.ZtreeEntity;
import com.yanglanting.disk.role.entity.SearchRoleEntity;
import com.yanglanting.disk.role.service.RoleService;
import com.yanglanting.disk.spring.control.BaseController;
import com.yanglanting.disk.user.exeption.UserOccupyException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;
    @Secured("ROLE_cldk_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @Secured("ROLE_cldk_data_role")
    @RequestMapping("/queryRoleInfoListByPage")
    @ResponseBody
    public TblResponseEntity<PtRoleEntity> queryRoleInfoListByPage(SearchRoleEntity entity, PagingEntity paging){
        TblResponseEntity<PtRoleEntity> entityTblResponseEntity = roleService.queryRoleInfoListByPage(entity,paging);
        return entityTblResponseEntity;
    }
    @Secured("ROLE_cldk_data_role_mod")
    @RequestMapping("/queryRoleInfoById")
    @ResponseBody
    public PtRoleEntity queryRoleInfoById(@RequestParam("roleUuid") String roleUuid){
        PtRoleEntity ptRoleEntity = roleService.queryRoleInfoById(roleUuid);
        return ptRoleEntity;
    }
    @Secured("ROLE_cldk_data_role_mod")
    @RequestMapping("/modifyRoleInfoById")
    @ResponseBody
    public String  modifyRoleInfoById(PtRoleEntity entity){
        roleService.modifyRoleInfoById(entity);
        return successJson("modify.role.success");
    }
    @Secured("ROLE_cldk_data_role_add")
    @RequestMapping("/addRoleInfoById")
    @ResponseBody
    public String  addRoleInfoById(PtRoleEntity entity){
        roleService.addRoleInfoById(entity);
        return successJson("add.role.success");
    }
    @Secured("ROLE_cldk_data_role_del")
    @RequestMapping("/deleteRoleByIds")
    @ResponseBody
    public String  deleteRoleByIds(@RequestParam("ids") String ids){
        try {
            roleService.deleteRoleByIds(ids);
        } catch (UserOccupyException e) {
//            e.printStackTrace();
            return failJson("role.allocate.user.fail",e.getMessage());
        } catch (MenuOccupyException e) {
            return failJson("role.allocate.menu.fail",e.getMessage());
//            e.printStackTrace();
        }
        return successJson("role.delete.success");
    }
    @Secured("ROLE_cldk_data_role_menu")
    @RequestMapping("/queryMenuTreeListByRole")
    @ResponseBody
    public List<ZtreeEntity> queryMenuTreeListByRole(@RequestParam("roleUuid") String roleUuid){
        List<ZtreeEntity> list = roleService.queryMenuTreeListByRole(roleUuid);
        return list;
    }

    @Secured("ROLE_cldk_data_role_menu")
    @RequestMapping("/saveRoleRefMenu")
    @ResponseBody
    public String saveRoleRefMenu(@RequestParam("roleUuidArray") String roleUuidArray,@RequestParam("menuIdArray") String menuIdArray){
        roleService.saveRoleRefMenu(roleUuidArray,menuIdArray);
        return successJson("role.allocate.permission.success");
    }

    /***yanglanting***/
    @RequestMapping("/queryUsersByRole")
    @ResponseBody
    public TblResponseEntity<PtUserEntity> queryUsersByRole(SearchRoleEntity entity, PagingEntity paging) {
        TblResponseEntity<PtUserEntity> responseEntity = roleService.queryUsersByRole(entity, paging);
        return responseEntity;
    }
    /***yanglanting***/

}
